package com.maidada.mddaichat.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.unfbx.chatgpt.OpenAiStreamClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.concurrent.TimeUnit;

/**
 * @author wulinxuan
 * @date 2025-04-17 12:07
 */
@Configuration
public class CacheConfig {

    /**
     * sse缓存(userId, sse)
     *
     * @return {@link Cache }<{@link String }, {@link SseEmitter }>
     */
    @Bean
    public Cache<String, SseEmitter> sseCache() {
        return Caffeine.newBuilder()
                .expireAfterWrite(30, TimeUnit.MINUTES)
                .maximumSize(100)
                .build();
    }

    /**
     * client缓存(userId, client)
     *
     * @return {@link Cache }<{@link String }, {@link OpenAiStreamClient }>
     */
    @Bean
    public Cache<String, OpenAiStreamClient> clientCache() {
        return Caffeine.newBuilder()
                .expireAfterWrite(30, TimeUnit.MINUTES)
                .maximumSize(100)
                .build();
    }
}
